<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>停止生成器</title>
</head>
<body>
<script>
    function* something() {
        try {
            var nextVal;
            while (true) {
                if (nextVal === undefined) {
                    nextVal = 1
                } else {
                    nextVal++
                }
                yield nextVal
            }
        } finally {
            console.log('clear generator')
        }
    }

    var it = something();
    for (var i of it) {
        if (i < 30) {
            console.log(i)
        } else {
            let result = it.return('end.'); // it.return会立即终止生成器，此时会运行finally语句
            console.log(result) // {result:'end.', done:true}
        }
    }
</script>
</body>
</html>
